<?php
/**
 * This file belongs to huakaili project
 *
 * @author jszhang
 */
require_once ( LIB_DIR.'model/ReviewDAO.php');
require_once ( LIB_DIR.'search/reviewsearch.php' );
require_once ( LIB_DIR.'classes/Pager.php' );

$reviews = array();
$reviewCount = 0;
$pageVars = array();

$sid= intval( $_GET['sid'] );
$pageVars['sid'] = $sid;

//check whether it is search request
$keyword = "";

$page = 1;
if( isset( $_GET['p'] ) ) {
    $page = intval( $_GET['p'] );
}

//default sortby
$sortby = 'review_date-desc';
$pageVars['sortby'] = $sortby;

$reviewdao = GetReviewDAO();
if( isset ( $_GET['q']) ) {

    $keyword = htmlspecialchars( $_GET['q'] );
    $pageVars['keyword'] = $keyword;

    $reviewsearch = new ReviewSearch();
    $searchType = FILTER_QUERY;

    if( isset( $_GET['sortby'] ) )
        $sortby = htmlspecialchars( $_GET['sortby'] );

    //determine search type
    $isDesc = false;
    if( !empty( $sortby ) ) {
        $pageVars['sortby'] = $sortby;

        //parse sort
        //price0 is sort by price asc and price1 is sortby price desc
        $sortbyArray = explode( "-", $sortby );
        if( count($sortbyArray) == 2 ) {
            $sortby = $sortbyArray[0];
            $isDesc = $sortbyArray[1] == "asc"? false : true;
        }
        else {
            $sortby = "";
        }

        if( $sortby != "" ) {
            if( $searchType == FILTER_QUERY ) {
                $searchType = FILTER_SORT_QUERY;
            }
        }
    }

    $filterArray = array();
    $filterArray['shop_id'] = array( intval($sid) );

    $result = array();
    switch( $searchType ) {
        case FILTER_QUERY:
            $result = $reviewsearch->MultiFilterQuery( $keyword, $filterArray, $page );
            break;
        case FILTER_SORT_QUERY:
            $result = $reviewsearch->MultiFilterAndSortQuery( $keyword, $filterArray, $sortby, $page, $isDesc );
            break;
    }

    if( $result[ 'total_found'] > 0 ) {        
        $reviewCount = $result[ 'total_found'];
        //database query detail information
        $reviews = $reviewdao->GetByIdList( $result[ 'matchIds'] );

        $highLighReviews = array();
        foreach( $reviews as $row ) {
	    $row['tags_plain'] = $row['tags'];

            //assemble field to highlight : tag, content
            $arrToHighlight = array
            (
                    //hard code here, need to be modified later
                    'content' => sconv( EdpSubStr($row['content'], 0, 200) ),
                    'tag' => sconv($row['tag'])
            );
            // merge database and high light result array
            $row = array_merge( $row, $reviewsearch->BuildHighLight( $keyword, $arrToHighlight ) );
            if( !empty($row['tag']) ) {//replace the tag highlight
		$row['tags'] = array();
		//replace the tag highlight
		foreach( $row['tags_plain'] as $strtag ){
			if( !empty($keyword) && strpos( $strtag, $keyword ) !== false){
				$row['tags'][] = str_replace( $keyword, "<span class=\"hilight\">".$keyword."</span>", $strtag );	
			}
			else{
				$row['tags'][] = $strtag;
			}	
		}
	    }
            $row['review_date'] = substr( $row['review_date'], 0, 10 );
            $highLighReviews[] = $row;
        }
        
	$pageVars['reviews'] = $highLighReviews;

        // prepare page bar of search result
	$currentURL = htmlspecialchars( $_SERVER["REQUEST_URI"] );
	$currentURL = preg_replace( '/[&\?]p=[0-9]+/', "", $currentURL );
	$pageObj = new Pager( $currentURL, $reviewCount, REVIEW_PER_PAGE, $page, UP_RESULT_PAGE );

        $pageVars['pagebar'] = $pageObj->getBar();
    }
}
//normal display
else {
    //normal database query
    $reviews = $reviewdao->GetByShopId( $sid, REVIEW_PER_PAGE, $page );
    $reviewCount = $reviewdao->GetReviewCountByShopId( $sid );

    if( $reviews ) {
        $pageVars['reviews'] = array();

        foreach( $reviews as $row ) {
            $row['content'] = sconv( EdpSubStr( $row['content'], 0, 200 ) );
            $row['tags'] = sconv( $row['tags'] );
	    $row['tags_plain'] = $row['tags'];

	    $pageVars['reviews'][] = $row;
	}

        // prepare page bar of search result
	$currentURL = htmlspecialchars( $_SERVER["REQUEST_URI"] );
        $currentURL = preg_replace( '/[&\?]p=[0-9]+/', "", $currentURL );
        $pageObj = new Pager( $currentURL, $reviewCount, REVIEW_PER_PAGE, $page, UP_RESULT_PAGE );

        $pageVars['pagebar'] = $pageObj->getBar();
    }
}
?>
